package com.battle.service.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.battle.pojo.entity.Organization;
import com.battle.pojo.entity.PersonWarCase;
import com.battle.pojo.entity.TrainingPersonSetting;
import com.battle.pojo.entity.User;
import com.battle.pojo.param.trainingPersonSetting.*;
import com.battle.pojo.vo.trainingPersonSetting.*;
import com.battle.pojo.dto.trainingPersonSetting.*;
import com.battle.service.repository.in.OrganizationRepository;
import com.battle.service.repository.in.PersonWarCaseRepository;
import com.battle.service.repository.in.TrainingPersonSettingRepository;
import com.battle.service.repository.in.UserRepository;
import com.battle.service.service.in.TrainingPersonSettingService;
import nirvana.core.domains.vo.PageResult;
import nirvana.core.utils.ids.IdGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 【描 述】：训练人员设置服务
 * 【环 境】：J2SE 17
 * 【详 细】：
 */
@Service
public class TrainingPersonSettingServiceImpl implements TrainingPersonSettingService {

    private final TrainingPersonSettingRepository trainingPersonSettingRepository;
    private final PersonWarCaseRepository personWarCaseRepository;
    private final OrganizationRepository organizationRepository;
    private final UserRepository userRepository;

    public TrainingPersonSettingServiceImpl(TrainingPersonSettingRepository trainingPersonSettingRepository, PersonWarCaseRepository personWarCaseRepository, OrganizationRepository organizationRepository, UserRepository userRepository) {
        this.trainingPersonSettingRepository = trainingPersonSettingRepository;
        this.personWarCaseRepository = personWarCaseRepository;
        this.organizationRepository = organizationRepository;
        this.userRepository = userRepository;
    }


    /**
     * 创建训练人员设置
     *
     * @param param
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTrainingPersonSetting(CreateTrainingPersonSettingParam param) {
        for (Long userId : param.getUserIds()) {
            PersonWarCase user = personWarCaseRepository.getByIdCheck(userId);
            if (trainingPersonSettingRepository.getById(userId) != null) {
                continue;
            }
            TrainingPersonSetting trainingPersonSetting = new TrainingPersonSetting();
            Long id = userId;
            trainingPersonSetting.setDepartmentId(param.getOrganizationId());
            trainingPersonSetting.setId(id);
            trainingPersonSetting.setUserId(userId);
            trainingPersonSettingRepository.create(trainingPersonSetting);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void bathRemoveTrainingPersonSetting(CreateTrainingPersonSettingParam param) {
        List<TrainingPersonSetting> trainingPersonSettings = trainingPersonSettingRepository.listByIds(param.getUserIds());
        if (CollectionUtil.isNotEmpty(trainingPersonSettings)) {
            trainingPersonSettingRepository.trueDeletionByIds(param.getUserIds());
        }
    }

    /**
     * 修改训练人员设置
     *
     * @param param
     */
    @Override
    public void editTrainingPersonSetting(EditTrainingPersonSettingParam param) {
        Long id = param.getId();
        TrainingPersonSetting trainingPersonSetting = trainingPersonSettingRepository.getByIdCheck(id);
        BeanUtils.copyProperties(param, trainingPersonSetting);
        trainingPersonSettingRepository.edit(trainingPersonSetting);
    }

    /**
     * 移除训练人员设置
     *
     * @param id
     */
    @Override
    public void removeTrainingPersonSetting(Long id) {
        trainingPersonSettingRepository.removeById(id);
    }

    /**
     * 启动/禁用训练人员设置
     *
     * @param id
     */
    @Override
    public void enabledTrainingPersonSetting(Long id) {
        TrainingPersonSetting trainingPersonSetting = trainingPersonSettingRepository.getByIdCheck(id);
        trainingPersonSetting.setIsDisable(!trainingPersonSetting.getIsDisable());
        trainingPersonSettingRepository.edit(trainingPersonSetting);
    }

    /**
     * 获取训练人员设置详情
     *
     * @param id
     * @return
     */
    @Override
    public GetTrainingPersonSettingVo getTrainingPersonSetting(Long id) {
        TrainingPersonSetting trainingPersonSetting = trainingPersonSettingRepository.getByIdCheck(id);
        GetTrainingPersonSettingVo vo = new GetTrainingPersonSettingVo();
        BeanUtils.copyProperties(trainingPersonSetting, vo);
        return vo;
    }

    /**
     * 获取训练人员设置 列表
     *
     * @param param
     * @return
     */
    @Override
    public PageResult<GetTrainingPersonSettingListVo> getTrainingPersonSettingList(GetTrainingPersonSettingListParam param) {

        SelectTrainingPersonSettingDto dto = new SelectTrainingPersonSettingDto();
        BeanUtils.copyProperties(param, dto);
        Page<GetTrainingPersonSettingListVo> page = trainingPersonSettingRepository.selectTrainingPersonSetting(dto);
        List<GetTrainingPersonSettingListVo> list = page.getRecords();

        if (CollectionUtil.isNotEmpty(list)){
            List<Long> userIds = new ArrayList<>();
            for (GetTrainingPersonSettingListVo vo : list) {
                userIds.add(vo.getCreator());
            }
            Map<Long, User> userMap = userRepository.getMap(userIds);
            List<Organization> listByEntity = organizationRepository.getListByEntity(new Organization());
            Map<Long, Organization> organizationMap = listByEntity.stream().collect(Collectors.toMap(Organization::getId, organization -> organization));

            for (GetTrainingPersonSettingListVo vo : list) {
                User create = userMap.get(vo.getCreator());
                if (create != null) {
                    vo.setCreateName(create.getName());
                }

                List<String> fullName=new ArrayList<>();
                String path = vo.getPath();
                if (path != null) {
                    String[] split = path.split(",");
                    for (String s : split) {
                        long organizationId = Long.parseLong(s);
                        Organization organization = organizationMap.get(organizationId);
                        if (organization != null) {
                            fullName.add(organization.getName());
                        }
                    }
                }
                vo.setFullName(fullName);
            }
        }

        return new PageResult<>(list, page);

    }

    /**
     * 获取训练人员设置下拉列表
     *
     * @param param
     * @return
     */
    @Override
    public List<GetTrainingPersonSettingDownListVo> getTrainingPersonSettingDownList(GetTrainingPersonSettingDownListParam param) {
        TrainingPersonSetting po = new TrainingPersonSetting();
        if (param != null) {
            BeanUtils.copyProperties(param, po);
        }
        List<TrainingPersonSetting> trainingPersonSettingList = trainingPersonSettingRepository.getListByEntity(po);

        List<GetTrainingPersonSettingDownListVo> list = new ArrayList<>();
        for (TrainingPersonSetting trainingPersonSetting : trainingPersonSettingList) {
            GetTrainingPersonSettingDownListVo vo = new GetTrainingPersonSettingDownListVo();
            BeanUtils.copyProperties(trainingPersonSetting, vo);
            list.add(vo);
        }
        return list;
    }


}



